import request from '@/utils/request'

// ==================== 课程管理 ====================

// 获取课程列表
export function getCourseList(params) {
  return request({
    url: '/v1/courses',
    method: 'get',
    params
  })
}

// 获取热门课程
export function getHotCourses(limit = 10) {
  return request({
    url: '/v1/courses/hot',
    method: 'get',
    params: { limit }
  })
}

// 获取推荐课程
export function getRecommendCourses(limit = 10) {
  return request({
    url: '/v1/courses/recommend',
    method: 'get',
    params: { limit }
  })
}

// 获取课程详情
export function getCourseDetail(id) {
  return request({
    url: `/v1/courses/${id}`,
    method: 'get'
  })
}

// 搜索课程
export function searchCourses(keyword, params) {
  return request({
    url: '/v1/courses/search',
    method: 'get',
    params: { keyword, ...params }
  })
}

// 获取课程分类
export function getCourseCategories() {
  return request({
    url: '/v1/course-categories',
    method: 'get'
  })
}

// 按分类获取课程
export function getCoursesByCategory(categoryId, params) {
  return request({
    url: `/v1/courses/category/${categoryId}`,
    method: 'get',
    params
  })
}

// 收藏课程
export function favoriteCourse(courseId) {
  return request({
    url: `/v1/courses/${courseId}/favorite`,
    method: 'post'
  })
}

// 取消收藏
export function unfavoriteCourse(courseId) {
  return request({
    url: `/v1/courses/${courseId}/unfavorite`,
    method: 'post'
  })
}

// 获取我的收藏课程
export function getFavoriteCourses(params) {
  return request({
    url: '/v1/courses/favorites',
    method: 'get',
    params
  })
}

// ==================== 章节管理 ====================

// 获取课程章节
export function getCourseChapters(courseId) {
  return request({
    url: `/v1/chapters/course/${courseId}`,
    method: 'get'
  })
}

// 获取章节详情
export function getChapterDetail(id) {
  return request({
    url: `/v1/chapters/${id}`,
    method: 'get'
  })
}

// ==================== 课时管理 ====================

// 获取章节课时
export function getChapterLessons(chapterId) {
  return request({
    url: `/v1/lessons/chapter/${chapterId}`,
    method: 'get'
  })
}

// 获取课时详情
export function getLessonDetail(id) {
  return request({
    url: `/v1/lessons/${id}`,
    method: 'get'
  })
}

// 开始学习课时
export function startLesson(lessonId) {
  return request({
    url: `/v1/lessons/${lessonId}/start`,
    method: 'post'
  })
}

// 完成课时
export function completeLesson(lessonId, watchTime) {
  return request({
    url: `/v1/lessons/${lessonId}/complete`,
    method: 'post',
    data: { watchTime }
  })
}

// 更新学习进度
export function updateLessonProgress(lessonId, progress, watchTime) {
  return request({
    url: `/v1/lessons/${lessonId}/progress`,
    method: 'post',
    data: { progress, watchTime }
  })
}

// ==================== 学习记录 ====================

// 获取学习记录
export function getStudyRecords(params) {
  return request({
    url: '/v1/study-records',
    method: 'get',
    params
  })
}

// 获取课程学习进度
export function getCourseProgress(courseId) {
  return request({
    url: `/v1/study-records/course/${courseId}/progress`,
    method: 'get'
  })
}

// 获取学习统计
export function getStudyStatistics() {
  return request({
    url: '/v1/study-records/statistics',
    method: 'get'
  })
}

// ==================== 课程评价 ====================

// 获取课程评价
export function getCourseReviews(courseId, params) {
  return request({
    url: `/v1/course-reviews/course/${courseId}`,
    method: 'get',
    params
  })
}

// 添加课程评价
export function addCourseReview(data) {
  return request({
    url: '/v1/course-reviews',
    method: 'post',
    data
  })
}

// 点赞评价
export function likeReview(reviewId) {
  return request({
    url: `/v1/course-reviews/${reviewId}/like`,
    method: 'post'
  })
}

// ==================== 课程报名 ====================

// 报名课程
export function enrollCourse(courseId) {
  return request({
    url: `/v1/enrollments/enroll`,
    method: 'post',
    data: { courseId }
  })
}

// 获取我的课程
export function getMyCourses(params) {
  return request({
    url: '/v1/enrollments/my-courses',
    method: 'get',
    params
  })
}

// 获取报名记录
export function getEnrollmentRecords(params) {
  return request({
    url: '/v1/enrollments',
    method: 'get',
    params
  })
}

// 检查是否已报名
export function checkEnrollment(courseId) {
  return request({
    url: `/v1/enrollments/check/${courseId}`,
    method: 'get'
  })
}

// ==================== 课程笔记 ====================

// 获取课程笔记
export function getCourseNotes(courseId, params) {
  return request({
    url: `/v1/course-notes/course/${courseId}`,
    method: 'get',
    params
  })
}

// 添加笔记
export function addCourseNote(data) {
  return request({
    url: '/v1/course-notes',
    method: 'post',
    data
  })
}

// 更新笔记
export function updateCourseNote(id, data) {
  return request({
    url: `/v1/course-notes/${id}`,
    method: 'put',
    data
  })
}

// 删除笔记
export function deleteCourseNote(id) {
  return request({
    url: `/v1/course-notes/${id}`,
    method: 'delete'
  })
}
